<?php

class woMenuBoxes extends qvaCore
{
	public $blogId = "";

	public function __construct()
	{
	    /*
	     * @get blog id
	     */
    	add_action('init', array($this, 'qva_get_blog_id'));
   
		add_action('init', array($this, 'qva_register_nav_menu'));
		add_action('admin_head', array($this, 'qva_create_qva_menu_boxes'));
		add_action('wp_ajax_add-qva-menu', array($this,'qva_add_menu'));
	}

	public function qva_walker_nav_menu_edit( $walker, $menu_id ) 
	{
    	return 'qva_Walker_Nav_Menu_Edit';
	}

 	public function qva_get_blog_id()
    {
        // $this->blogId = get_option($this->keyBlogId);
    }

	public function qva_register_nav_menu()
	{
		register_nav_menus( array('qva-menu' => 'QVA Menu') );
	}

	public function qva_create_qva_menu_boxes()
	{
		add_meta_box('qva-qva-menu-box-id', 'QVA Menu', array($this, 'qva_menu_box_builder'), 'nav-menus', 'side', 'high');
	}

	public function qva_menu_box_builder()
	{
		include (QVA_ADMIN_MB . 'menu/menu-builder.php');
	}


	public function qva_add_menu()
	{
	    $item_ids = array();
	    $aqvaMenu = isset($_POST['qva']) ? $_POST['qva'] : '';

	    $menu_id = 2;
	    $_actual_db_id = 0;

	    require_once( dirname(__FILE__) . '/wo-edit-menu.php' );

	    if ( !empty($aqvaMenu) )
	    {
	        foreach ($aqvaMenu as $k => $getChecked)
	        {
	            if ( isset($getChecked['menu-item-checked']) && !empty($getChecked['menu-item-checked']) )
	            {
	                $args = array(
	                	'menu-item-object-id'	=> ( isset( $getChecked['menu-item-checked'] ) ? $getChecked['menu-item-checked'] : '' ),
	                	'menu-item-object'	 	=> ( isset( $getChecked['menu-item-object'] ) ? $getChecked['menu-item-object'] : '' ),
	                	'menu-item-db-id' 		=> ( isset( $getChecked['menu-item-db-id'] ) ? $getChecked['menu-item-db-id'] : '' ),
	                    'menu-item-title' 		=> ( isset( $getChecked['menu-item-title'] ) ? $getChecked['menu-item-title'] : '' ),
	                    'menu-item-url'  	 	=> ( isset( $getChecked['menu-item-url'] ) ? $getChecked['menu-item-url'] : '' ),
	                    'menu-item-type'  		=> ( isset( $getChecked['menu-item-type'] ) && ( 'custom' != $getChecked['menu-item-object'] ) ? $getChecked['menu-item-type'] : 'custom' ),
	                   	// 'menu-item-object' 		=> ( isset( $getChecked['menu-item-object'] ) ? $getChecked['menu-item-object'] : '' ),
	                   	'menu-item-parent-id' 	=> ( isset( $getChecked['menu-item-parent-id'] ) ? $getChecked['menu-item-parent-id'] : '' ),
	                   	'menu-item-attr-title' 	=> ( isset( $getChecked['menu-item-attr-title'] ) ? $getChecked['menu-item-attr-title'] : '' ),
	                   	'menu-item-classes' 	=> '',
	                   	'menu-item-xfn' 		=> ( isset( $getChecked['menu-item-xfn'] ) ? $getChecked['menu-item-xfn'] : '' ),
                        'menu-item-section-name'=> ( isset( $getChecked['menu-item-section-name'] ) ? $getChecked['menu-item-section-name'] : '' ),
	                );
	                $item_ids[] = $args;

 	            }
	        }
	   
	        $item_ids = $this->qva_wp_save_nav_menu_items( 0, $item_ids );
	        
	        if ( is_wp_error( $item_ids ) )
	            wp_die( 0 );

	        $menu_items = array();

	        foreach ( (array) $item_ids as $menu_item_id )
	        {
	            $menu_obj = get_post( $menu_item_id );
	            if ( ! empty( $menu_obj->ID ) ) {
	                $menu_obj = wp_setup_nav_menu_item( $menu_obj );
	                $menu_obj->label = $menu_obj->title; // don't show "(pending)" in ajax-added items
	                $menu_items[] = $menu_obj;
	            }
	        }



	        $walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'qva_Walker_Nav_Menu_Edit', $_POST['menu'] );

	        if ( ! class_exists( $walker_class_name ) )
	            wp_die(0);

	        if ( ! empty( $menu_items ) ) {
	            $args = array(
	                'after' => '',
	                'before' => '',
	                'link_after' => '',
	                'link_before' => '',
	                'walker' => new $walker_class_name,
	            );
	            echo walk_nav_menu_tree( $menu_items, 0, (object) $args );
	        }
    	}
    
		die();
	}

	public function qva_wp_save_nav_menu_items( $menu_id = 0, $menu_data = array() ) 
	{
	    $menu_id = (int) $menu_id;
	    $items_saved = array();

	    if ( 0 == $menu_id || is_nav_menu( $menu_id ) ) 
	    {
	    	
	        // Loop through all the menu items' POST values
	        foreach( (array) $menu_data as $_possible_db_id => $_item_object_data ) {
	            if (
	            	 empty( $_item_object_data['menu-item-object-id'] ) && // checkbox is not checked 
	            	(
	                    ! isset( $_item_object_data['menu-item-type'] ) || // and item type either isn't set
	                    in_array( $_item_object_data['menu-item-url'], array( 'http://', '' ) ) || // or URL is the default
	                    ! ( 'custom' == $_item_object_data['menu-item-type'] && ! isset( $_item_object_data['menu-item-db-id'] ) ) || // or it's not a custom menu item (but not the custom home page)
	                    ! empty( $_item_object_data['menu-item-db-id'] ) // or it *is* a custom menu item that already exists
	                )

	            ) {
	                continue; // then this potential menu item is not getting added to this menu
	            }

	            // if this possible menu item doesn't actually have a menu database ID yet
	            if (
	                empty( $_item_object_data['menu-item-db-id'] ) ||
	                ( 0 > $_possible_db_id ) ||
	                $_possible_db_id != $_item_object_data['menu-item-db-id']
	            ) {
	                $_actual_db_id = 0;
	            } else {
	                $_actual_db_id = (int) $_item_object_data['menu-item-db-id'];
	            }

	            $args = array(
	            	'menu-item-object-id'	=> ( isset( $_item_object_data['menu-item-object-id'] ) ? $_item_object_data['menu-item-object-id']: '' ),
	            	'menu-item-db-id' 		=> ( isset( $_item_object_data['menu-item-db-id'] ) ? $_item_object_data['menu-item-db-id'] : '' ),
	               	'menu-item-object'	 	=> ( isset( $_item_object_data['menu-item-object'] ) ? $_item_object_data['menu-item-object'] : '' ),
	               	'menu-item-parent-id' 	=> ( isset( $_item_object_data['menu-item-parent-id'] ) ? $_item_object_data['menu-item-parent-id'] : '' ),
	                'menu-item-type' 		=> ( isset( $_item_object_data['menu-item-type'] ) ? $_item_object_data['menu-item-type'] : '' ),
	                'menu-item-title' 		=> ( isset( $_item_object_data['menu-item-title'] ) ? $_item_object_data['menu-item-title'] : '' ),
	                'menu-item-url' 		=> ( isset( $_item_object_data['menu-item-url'] ) ? $_item_object_data['menu-item-url'] : '' ),
	               	'menu-item-attr-title' 	=> ( isset( $_item_object_data['menu-item-attr-title'] ) ? $_item_object_data['menu-item-attr-title'] : '' ),
	               	'menu-item-classes' 	=> '',
	               	'menu-item-xfn' 		=> ( isset( $_item_object_data['menu-item-xfn'] ) ? $_item_object_data['menu-item-xfn'] : '' ),
	            );

				// var_dump($args);

                $orderSection[] = ( isset( $_item_object_data['menu-item-section-name'] ) ? $_item_object_data['menu-item-section-name'] : '' );

                $items_saved[] = wp_update_nav_menu_item( $menu_id, $_actual_db_id, $args );

	        }

            

	    }
    	return $items_saved;
	}
}

$initwoMenuBoxes = new woMenuBoxes();